home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gchess40.lha / gnuchess4.0p62 / src / postprint.c < prev    next >
C/C++ Source or Header  |  1993-06-22  |  5KB  |  217 lines

  1. /*
  2.  * postprint.c - C source for GNU CHESS
  3.  *
  4.  * Copyright (c) 1992 Free Software Foundation
  5.  *
  6.  * This file is part of GNU CHESS.
  7.  *
  8.  * GNU Chess is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2, or (at your option)
  11.  * any later version.
  12.  *
  13.  * GNU Chess is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with GNU Chess; see the file COPYING.  If not, write to
  20.  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22. #include <stdio.h>
  23. #include "gnuchess.h"
  24. #if !defined HASHFILE
  25. #ifdef MSDOS
  26. #define HASHFILE "gnuchess.has"
  27. #else
  28. #define HASHFILE "/usr/local/lib/gnuchess.hash"
  29. #endif
  30. #endif
  31. #ifdef MSDOS
  32. #include <stdlib.h>
  33. #include <string.h>
  34. #include <time.h>
  35. #define RWA_ACC "r+b"
  36. #define WA_ACC "w+b"
  37. #else
  38. #define RWA_ACC "r+"
  39. #define WA_ACC "w+"
  40. #include <sys/param.h>
  41. #include <sys/types.h>
  42. #endif /* MSDOS */
  43. FILE *hashfile;
  44.  
  45. #define truescore 0x0001
  46. #define lowerbound 0x0002
  47. #define upperbound 0x0004
  48. #define kingcastle 0x0008
  49. #define queencastle 0x0010
  50.  
  51. long i, j;
  52. int nr[MAXDEPTH];
  53. struct fileentry n;
  54. int r, c;
  55. char line[128];
  56. char *l;
  57. short int t;
  58. int cc1, cc2;
  59. char mvstr[3][6];
  60. short int board[64];
  61. void
  62. algbr (short int f, short int t, short int flag)
  63.  
  64. /*
  65.  * Generate move strings in different formats.
  66.  */
  67.  
  68. {
  69.   int m3p;
  70.  
  71.   if (f != t)
  72.     {
  73.       /* algebraic notation */
  74.       mvstr[0][0] = Cxx[column (f)];
  75.       mvstr[0][1] = Rxx[row (f)];
  76.       mvstr[0][2] = Cxx[column (t)];
  77.       mvstr[0][3] = Rxx[row (t)];
  78.       mvstr[0][4] = mvstr[3][0] = '\0';
  79.       if (((mvstr[1][0] = Pxx[board[f]]) == 'P') || (flag & promote))
  80.     {
  81.       if (mvstr[0][0] == mvstr[0][2])
  82.         {            /* pawn did not eat */
  83.           mvstr[2][0] = mvstr[1][0] = mvstr[0][2];    /* to column */
  84.           mvstr[2][1] = mvstr[1][1] = mvstr[0][3];    /* to row */
  85.           m3p = 2;
  86.         }
  87.       else
  88.         /* pawn ate */
  89.         {
  90.           mvstr[2][0] = mvstr[1][0] = mvstr[0][0];    /* from column */
  91.           mvstr[2][1] = mvstr[1][1] = mvstr[0][2];    /* to column */
  92.           mvstr[2][2] = mvstr[0][3];
  93.           m3p = 3;        /* to row */
  94.         }
  95.       mvstr[2][m3p] = mvstr[1][2] = '\0';
  96.       if (flag & promote)
  97.         {
  98.           mvstr[0][4] = mvstr[1][2] = mvstr[2][m3p] = Qxx[flag & pmask];
  99.           mvstr[1][3] = mvstr[2][m3p + 1] = mvstr[0][5] = '\0';
  100.         }
  101.     }
  102.       else
  103.     /* not a pawn */
  104.     {
  105.       mvstr[2][0] = mvstr[1][0];
  106.       mvstr[2][1] = mvstr[0][1];
  107.       mvstr[2][2] = mvstr[1][1] = mvstr[0][2];    /* to column */
  108.       mvstr[2][3] = mvstr[1][2] = mvstr[0][3];    /* to row */
  109.       mvstr[2][4] = mvstr[1][3] = '\0';
  110.       strcpy (mvstr[3], mvstr[2]);
  111.       mvstr[3][1] = mvstr[0][0];
  112.       if (flag & cstlmask)
  113.         {
  114.           if (t > f)
  115.         {
  116.           strcpy (mvstr[1], "o-o");
  117.           strcpy (mvstr[2], "O-O");
  118.         }
  119.           else
  120.         {
  121.           strcpy (mvstr[1], "o-o-o");
  122.           strcpy (mvstr[2], "O-O-O");
  123.         }
  124.         }
  125.     }
  126.     }
  127.   else
  128.     mvstr[0][0] = mvstr[1][0] = mvstr[2][0] = mvstr[3][0] = '\0';
  129. }
  130.  
  131. void
  132. main (int argc, char **argv)
  133. {
  134.   int f = 0;
  135.   char flbuf[10];
  136.   char *fl;
  137.  
  138.   if ((hashfile = fopen (HASHFILE, RWA_ACC)) == NULL)
  139.     exit (1);
  140.   for (i = 0; i < MAXDEPTH; i++)
  141.     nr[i] = 0;
  142.   fseek (hashfile, 0L, SEEK_END);
  143.   i = ftell (hashfile) / sizeof (struct fileentry);
  144.   fseek (hashfile, 0L, SEEK_SET);
  145.   printf ("/V 11 72 mul def /L 60 def\n");
  146.   for (j = 0; j < i; j++)
  147.     {
  148.       fread (&n, sizeof (struct fileentry), 1, hashfile);
  149.       if (n.depth)
  150.     {
  151.       nr[0]++;
  152.       if (nr[0] == 19)
  153.         {
  154.           nr[0] = 1;
  155.           printf ("showpage\n/V 11 72 mul def\n");
  156.           printf ("/L 60 def\n");
  157.           f = 0;
  158.         }
  159.       /* now process this entry */
  160.       strcpy (line, "C ('#[");
  161.       for (r = 0; r < 8; r++)
  162.         {
  163.           l = line + 6 + (7 - r) * 9;
  164.           for (c = 0; c < 4; c++)
  165.         {
  166.           cc1 = (n.bd[r * 4 + c] >> 4) & 0xf;
  167.           cc2 = n.bd[r * 4 + c] & 0xf;
  168.           board[r * 8 + c * 2] = (int) cc1 & 0x7;
  169.           board[r * 8 + c * 2 + 1] = (int) cc2 & 0x7;
  170.           if (cc1 & 0x8)
  171.             *l++ = Qxx[cc1 & 0x7];
  172.           else
  173.             *l++ = Pxx[cc1 & 0x7];
  174.           if (cc2 & 0x8)
  175.             *l++ = Qxx[cc2 & 0x7];
  176.           else
  177.             *l++ = Pxx[cc2 & 0x7];
  178.         }
  179.           *l++ = ';';
  180.         }
  181.       l--;
  182.       line[79] = '\0';
  183.       strcat (line, "]') show");
  184.       algbr (n.f, n.t, 0);
  185.       t = (n.sh << 8) + n.sl;
  186.       /* decode flags */
  187.       fl = flbuf;
  188.       if (n.flags & kingcastle)
  189.         *fl++ = 'k';
  190.       if (n.flags & queencastle)
  191.         *fl++ = 'q';
  192.       if (n.flags & truescore)
  193.         *fl++ = 't';
  194.       if (n.flags & lowerbound)
  195.         *fl++ = 'l';
  196.       if (n.flags & upperbound)
  197.         *fl++ = 'u';
  198.       *fl = '\0';
  199.       printf ("L V moveto\n");
  200.       printf ("R (%s flags %s depth %d score %d", mvstr[0], flbuf, n.depth, t);
  201.       printf (") show\n");
  202.       printf ("L  V 100 sub moveto\n");
  203.       printf ("%s\n", line);
  204.       f++;
  205.       if (f == 3)
  206.         {
  207.           printf ("/V V 120 sub def /L 60 def\n");
  208.           f = 0;
  209.         }
  210.       else
  211.         printf ("/L 160 L add def\n");
  212.     }
  213.     }
  214.   if (nr[0])
  215.     printf ("showpage\n");
  216. }
  217.